
var allpagedata = new Array();
var screen_width = $(window).width();
var screen_height = $(window).height();
//半透明层
var opacity = '<div class="opacity"></div>';
//编辑弹出框
var album_edit = '';
album_edit += '<div class="edit_box" style="width:390px;background-color:#fff;position:fixed;top:130px;z-index:10000">';
album_edit += '<div class="close"><a href="" class="cancel"></a></div><div class="tm dib"style="position:fixed;top:130px;z-index:-1"></div>';
album_edit += '<div style="background-color:#fff;"><div class="edit_title 545454" style="color:#555;font-size:16px;font-weight:bold;padding:5px 10px;">编辑相册</div><div class="in_h1 mt10" style="margin-left:20px;font-size:16px;color:#666;">相册名&nbsp&nbsp<input type="text" id="edit_title" style="color:#666;font-size:16px;line-height:20px;padding:5px;height:20px;width:240px;border:1px solid #BABABA"></div>';
album_edit += '<div class="save_delete"><span class="save dib su_de fl" style="cursor:pointer;margin:0 10px 0 0;">保&nbsp存</span><span class="del_album  su_de dib fl" style="cursor:pointer;">删&nbsp除</span></div>';
album_edit += '</div></div>';

$(document).ready(function(){

	var renum=1;
	
	var scrwidth = $(window).width();
	scrwidth -= 20;
	var scrw = parseInt(scrwidth/240);
	scrwidth = scrw*240;
	var lihtml = $("#stage li:first").html();
	$("#stage").css("width",scrwidth).empty();
	for(var i=0;i<scrw;i++){
		$("#stage").append("<li></li>");
	}
	$("#stage li:first").append(lihtml);
	allpagedata.push(lihtml)
	loadMore(renum);
	

//$(window).scroll(function(){
//	// 当滚动到最底部以上100像素时， 加载新内容
//	if ($(document).height() - $(this).scrollTop() - $(this).height()<100) loadMore(++renum);
//});


function loadMore(num)
{

	$.ajax({
		url : "user_album.class.php",
		data:{renum:num,request:1,user:$("#js_getuser").text()},
		dataType : 'json',
		success : function(json){
			if(typeof json == 'object'){
				var oProduct, $row, iHeight, iTempHeight;
				for(var i=0, l=json.length; i<l; i++){
					oProduct = json[i];					
					// 找出当前高度最小的列, 新内容添加到该列
					iHeight = -1;
					$('#stage li').each(function(){
						iTempHeight = Number( $(this).height() );
						if(iHeight==-1 || iHeight>iTempHeight){
							iHeight = iTempHeight;
							$row = $(this);
						}
					});
					//alert(iHeight);
					var temp = '<div class="waterfall dib"><input type="hidden" value="'+json[i][0].id+'" class="this_id"><h3 class="al_title" style="color:#777">'+json[i][0].gallery+'</h3><a href="user_info.php?id='+json[i][0].id+'" class="dib mb10 mt5">';
					for(var n=1;n<10;n++){
						if(typeof(json[i][n])!="undefined"){
							temp += '<div class="al_po"><img width="63px" height="63px" src="'+json[i][n].img+'"></div>';
						}else{
							temp += '<div class="al_po"></div>';
						}
					}
					if(json[i][0].edit==1){
						temp += '<a href="" class="care_button dib album_edit" style="">编&nbsp&nbsp&nbsp辑</a></div>';
					}else{
						temp += '<a href="user_info.php?id='+json[i][0].id+'" class="care_button dib" style="">查&nbsp&nbsp&nbsp看</a></div>';
					}
					$item=$(temp).hide();
					
					$row.append($item);
					$item.fadeIn();
					
					allpagedata.push(temp);
				}
			}

	},
	error:function(err){
		//alert(err);
		}
	});
	
}

});	

scrwidth = $(window).width();
setInterval(function(){
	scrwidth = wresize(scrwidth);
},50);

function wresize(scrwidth){
	//alert(scrwidth);
	var new_scrwidth = $(window).width();
	if(Math.abs(scrwidth - new_scrwidth) > 50){
		var scrolltop = $(document).scrollTop();
		var documentheight = $(document).height();
		scrwidth -= 20;
		var scrw_old = parseInt(scrwidth/240);
  	
  	$("#stage").empty();
  	var scrwidth = $(window).width();
		scrwidth -= 20;
		var scrw = parseInt(scrwidth/240);
		scrwidth = scrw*240;
		$("#stage").css("width",scrwidth);
		for(var i=0;i<scrw;i++){
			$("#stage").append("<li></li>");
		}
  	
  	var row,iHeight,iTempHeight;
  	for(var i=0, l=allpagedata.length; i<l; i++){
			iHeight = -1;
		$('#stage li').each(function(){
			iTempHeight = Number( $(this).height() );
			if(iHeight==-1 || iHeight>iTempHeight){
				iHeight = iTempHeight;
				row = $(this);
			}
		});
		
			var $item=$(allpagedata[i]).fadeTo(1,0);		
			row.append($item);
			$item.fadeTo(600,1);
		}
	setTimeout(function(){
		var new_scroll = $(document).height()*scrolltop/documentheight;
		//alert(scrolltop+"-"+documentheight+"-"+new_scroll);
		$("body,html").animate({scrollTop:new_scroll},300);
	},600);
	}
	return new_scrwidth;
}

$(".album_edit").live('click',function(){
	thisobj = $(this);
	$("body").append(opacity).append(album_edit).css({overflow:"hidden"});
	$(".opacity").css({"width":$(window).width()+100+"px","height":$(window).height()+100+"px","position":"fixed","background-color":"#ccc","opacity":".8","left":"0px","top":"0px","z-index":"999"});
	$(".tm").css({left:screen_width/2-235+"px",top:$(window).scrollTop()+115+"px"}).animate({top:$(window).scrollTop()+135+"px"},300);
	$(".edit_box").css({left:screen_width/2-220+"px",top:$(window).scrollTop()+130+"px"}).animate({top:$(window).scrollTop()+150+"px"},300);
	$("#edit_title").val(thisobj.parent().find(".al_title").text());
	$(".save").one('click',function(){
		if($("#edit_title").val() != ""){
			$.ajax({url:"user_album.class.php",data:{request:2,album_name:$("#edit_title").val(),id:thisobj.parent().find(".this_id").val()},type:"POST",async:false,success:function(re){
				if(re == "yes"){
					$(".edit_box").remove();
					$(".opacity").remove();
					message("修改成功");
					setTimeout(function(){
						location.href=location.href;
					},1000);
				}
			}});
		}
	});
	$(".del_album").live("click",function(){
		if(confirm("确认删除这个相册吗？删除后这个相册内所有的图片都将被删除！")){
			$.ajax({url:"user_album.class.php",data:{request:3,id:thisobj.parent().find(".this_id").val()},async:false,success:function(re){
				if(re == "yes"){
					$(".edit_box").remove();
					$(".opacity").remove();
					message("删除成功");
					location.href=location.href;
				}
			}});
		}
	});
	
	return false;
});